package org.example.cicdi.service.impl.third.fieldcondition;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;

import org.example.cicdi.service.impl.second.AuxHelper;
import org.example.codegenerate.modelext.WebVo.Enum.EnumJavaMappingDbType;
import org.example.codegenerate.modelext.WebVo.domian.column.ColumnVoDomain;
import org.example.codegenerate.modelext.WebVo.domian.column.DbVo;
import org.example.codegenerate.modelext.WebVo.domian.table.WebModelVo;
import org.springframework.stereotype.Component;

import javax.persistence.Column;
import javax.persistence.Id;
import java.lang.reflect.Field;
import java.util.List;
@Slf4j
@Component
public class HasColumnNoManyToOneNoJoinColumn extends AbstractColumnAndManyToOne{
    public HasColumnNoManyToOneNoJoinColumn(){
        localfieldCondition.setHasorNotJoinColumn(EnumConsantExistFieldType.NoJoinColumn.getStatus());
        localfieldCondition.setHasorNotJpaManyToOne(EnumConsantExistFieldType.NoManyToOne.getStatus());
        localfieldCondition.setHasorNotColumn(EnumConsantExistFieldType.HasColumn.getStatus());
    }
    @Override
    protected void execute(Field field, ColumnVoDomain column, WebModelVo webModelVo, List<WebModelVo> webModelVoList) {
        DbVo db = column.getDbVo();

       // if (field.getAnnotation(Column.class) != null) {
            Column vo = field.getAnnotation(Column.class);
            db.setDbDefaultVal(null);
            if(vo.name().length()>1) {
                db.setDbFieldName(vo.name());
            }else {
                db.setDbFieldName(field.getName());
            }

            String comment = vo.columnDefinition().toLowerCase();
            if (StringUtils.isNotBlank(comment)) {
                db.setDbFieldTxt(comment.substring(comment.indexOf("'") + 1, comment.length() - 1));
            }else {
                db.setDbFieldTxt(field.getName());
            }

            //db.setDbIsKey(vo.unique() ? 1 : 0);
            if(field.getAnnotation(Id.class) != null){
                db.setDbIsKey(1);
                db.setDbIsNull(0);
            }else {
                db.setDbIsKey(0);
                db.setDbIsNull(vo.nullable() ? 1 : 0);
            }

            db.setDbLength(vo.length());
            db.setDbPointLength(vo.precision());

            //setFieldDbType(field,vo,db);
            setFieldDbTypeExt(field,vo,db);
            db.setDbPointLength(vo.scale());

            //special设置，没有从注解上进行设置
            db.setDbDefaultVal(null);
            db.setValidateVo(null);

            //db.setFieldName(oConvertUtils.camelName(db.getDbFieldName()));
            db.setFieldName(db.getDbFieldName());
            //db.setFieldType(new OnlCgformHeadService10Impl().getWrapper(db.getDbType()));
            db.setFieldType(AuxHelper.getWrapper(db.getDbType()));

            column.setDbVo(db);
        }

    @Override
    protected void setFieldDbTypeExt(Field field, DbVo db) {
        //如果columnDefinition，不是采用默认值。因为默认值就是""，如果写错了，JPA在实例化的时候，就提示了
        EnumJavaMappingDbType enumJavaMappingDbType;
        /*
                    enumJavaMappingDbType = EnumJavaMappingDbType.getBycolumnDefinition(field.getType().getSimpleName());
                    //assert 布尔表达式 ：“错误信息提示”；
                    assert enumJavaMappingDbType != null : "Column注解中字段的类型不在系统的支持范围之内，当前系统仅支持8种类型,varchar,text,blob,date,datetime,decimal,int,double";*/
        //log.info("当前正常处理的字段类型：{}，字段的名称为{}",enumJavaMappingDbType,field.getName(),enumJavaMappingDbType.getDbname());
			/*//先特殊处理一下，针对DateTime的，全部转为Date
			if(enumJavaMappingDbType.getDbname().equalsIgnoreCase("datetime")){
				db.setDbType("date");
			}*/

        //如果有Column，但是在这个上面没有columnDefinition，那么就采用默认的字段类型。但是字段类型必须在6种之内，String，Date，Decimal，Integer，Double
        enumJavaMappingDbType = EnumJavaMappingDbType.getByDbJavaTypeName(field.getType().getSimpleName());
        assert enumJavaMappingDbType != null : "Java中定义的字段的类型不在系统的支持范围之内，当前系统仅支持6种Java类型,String," +
                "Date,BigDecimal,Integer,Double";


        db.setDbType(enumJavaMappingDbType.getCurrentFrameWorkNeededDbType());

    }

    //return column;
    //}
}
